import math
import cv2

WRIST = 0
THUMB=4
INDEX=8
MIDDLE=12
FINGER=16
PINKY=20

def points_cos_angle(point1, point2):
    try:
        angle_ = round(math.degrees(math.acos((point1[0] * point2[0] + point1[1] * point2[1]) / (((point1[0] ** 2 + point1[1] ** 2) * (point2[0] ** 2 + point2[1] ** 2)) ** 0.5)))/1.8,0)
    except:
        angle_ = 65535.
    if angle_ > 180.:
        angle_ = 65535.
    return angle_

def get_fingers_angle(handPoints_list):
    # 利用mediapipe的手部关键点数组构建相关向量并传入函数中计算两个二维向量之间的角度,最后将结果置入列表中
    angle_list = []
    # ---------------------------- thumb 大拇指角度
    angle_ = points_cos_angle(
        ((int(handPoints_list[0][0]) - int(handPoints_list[2][0])),
         (int(handPoints_list[0][1]) - int(handPoints_list[2][1]))),
        ((int(handPoints_list[3][0]) - int(handPoints_list[4][0])),
         (int(handPoints_list[3][1]) - int(handPoints_list[4][1])))
    )
    angle_list.append(angle_)
    # ---------------------------- index 食指角度
    angle_ = points_cos_angle(
        ((int(handPoints_list[0][0]) - int(handPoints_list[6][0])),
         (int(handPoints_list[0][1]) - int(handPoints_list[6][1]))),
        ((int(handPoints_list[7][0]) - int(handPoints_list[8][0])),
         (int(handPoints_list[7][1]) - int(handPoints_list[8][1])))
    )
    angle_list.append(angle_)
    # ---------------------------- middle 中指角度
    angle_ = points_cos_angle(
        ((int(handPoints_list[0][0]) - int(handPoints_list[10][0])),
         (int(handPoints_list[0][1]) - int(handPoints_list[10][1]))),
        ((int(handPoints_list[11][0]) - int(handPoints_list[12][0])),
         (int(handPoints_list[11][1]) - int(handPoints_list[12][1])))
    )
    angle_list.append(angle_)
    # ---------------------------- ring 无名指角度
    angle_ = points_cos_angle(
        ((int(handPoints_list[0][0]) - int(handPoints_list[14][0])),
         (int(handPoints_list[0][1]) - int(handPoints_list[14][1]))),
        ((int(handPoints_list[15][0]) - int(handPoints_list[16][0])),
         (int(handPoints_list[15][1]) - int(handPoints_list[16][1])))
    )
    angle_list.append(angle_)
    # ---------------------------- pink 小拇指角度
    angle_ = points_cos_angle(
        ((int(handPoints_list[0][0]) - int(handPoints_list[18][0])),
         (int(handPoints_list[0][1]) - int(handPoints_list[18][1]))),
        ((int(handPoints_list[19][0]) - int(handPoints_list[20][0])),
         (int(handPoints_list[19][1]) - int(handPoints_list[20][1])))
    )
    angle_list.append(angle_)
    return angle_list